//
//  htvmycollection.m
//  WZYApp
//
//  Created by zhangwq on 16/1/28.
//  Copyright © 2016年 HT. All rights reserved.
//

#import "htvmycollection.h"
#import "HTFrameKit.h"
#import "HTSysKit.h"
#import "HTFrameConvert.h"
#import "UIImageView+WebCache.h"

@interface htvmycollection (){
    CGFloat width;
    CGFloat height;
    UIColor *backColor;
    UIView *uv;
    NSMutableArray *itemArray;
    UIWebView *webview;
}

@property (nonatomic, strong) NSOperationQueue *queue;

@end

@implementation htvmycollection

- (NSOperationQueue *)queue {
    if (!_queue) _queue = [[NSOperationQueue alloc] init];
    return _queue;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    width = size.width;
    height = size.height;
    
    [self loading];
    
    itemArray=[NSMutableArray array];
    uv=[[UIView alloc]init];
    backColor=[UIColor colorWithRed:150.0/255 green:150.0/255 blue:150.0/255 alpha:1.0f];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    self.tableView.separatorStyle=NO;
    
    [self BindList];
    
}


-(void)loading{
    
    self.tableView.scrollEnabled=NO;
    
    webview=[[UIWebView alloc]initWithFrame:CGRectMake(-10, 0, width+10, height)];
    webview.backgroundColor=[UIColor colorWithRed:255.0/255 green:255.0/255 blue:255.0/255 alpha:1.0f];
    webview.opaque=NO;
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *resPath = [bundle resourcePath];
    NSString *filePath = [resPath stringByAppendingPathComponent:@"loading.html"];
    
    [webview loadHTMLString:[NSString stringWithContentsOfFile:filePath]
                    baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];
    [self.tableView addSubview:webview];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


//刷新数据
-(void)BindList
{
    NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
    HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
    
    NSDictionary *dict = @{@"memberid":[HTSysParam userID],@"pagernationVo": @{@"currPage": @1,@"pageSize": @100}};
    [htnet hmNetWCFmethodName:@"web/memberItemCollection/queryDataMemberItemCollectionByMember" andDic:dict complete:^(NSString *backstr)
     {
         NSDictionary *rootDict=[HTFrameNet hmConvertDictFromJsonStr:backstr];
         NSDictionary *body=[rootDict objectForKey:@"body"];
         if(body!=nil){
             
             NSDictionary *SaleMemberOrder=[body objectForKey:@"DataMemberItemCollection"];
             itemArray=[SaleMemberOrder objectForKey:@"data"];
             [self.tableView reloadData];
             
             self.tableView.scrollEnabled=YES;
             [webview removeFromSuperview];
             
             
         }
         
         
         
         
     }];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [itemArray count];
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0;
}


- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
    
    NSString *cellId = @"cellname" ;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :cellId];
    if (cell == nil ) {
        cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :cellId] ;
    }
    
    NSDictionary *dict=[itemArray objectAtIndex:[indexPath row]];
    
    uv=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 100)];
    
    
    UILabel *lbitemcode=[[UILabel alloc]initWithFrame:CGRectMake(110, 10, 200, 20)];
    lbitemcode.text=[[NSString alloc]initWithFormat:@"编号：%@",[self checkValue:[dict objectForKey:@"itemcode"]] ];
    lbitemcode.textColor=backColor;
    lbitemcode.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbitemcode];
    
    UILabel *lbitemname=[[UILabel alloc]initWithFrame:CGRectMake(110, 30, 200, 20)];
    lbitemname.text=[[NSString alloc]initWithFormat:@"品名：%@",[self checkValue:[dict objectForKey:@"itemname"]] ];
    lbitemname.textColor=backColor;
    lbitemname.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbitemname];
    

    
    UILabel *lbitemstd=[[UILabel alloc]initWithFrame:CGRectMake(110, 50, 200, 20)];
    lbitemstd.text=[[NSString alloc]initWithFormat:@"规格：%@",[self checkValue:[dict objectForKey:@"itemstd"]] ];
    lbitemstd.textColor=backColor;
    lbitemstd.font=[UIFont fontWithName:@"Arial" size:11.0];
    [uv addSubview:lbitemstd];
    
    
    if([[dict objectForKey:@"specialPrice"]floatValue]>0){
        
        UILabel *lbcolorname=[[UILabel alloc]initWithFrame:CGRectMake(110, 70, 200, 20)];
        lbcolorname.text=[[NSString alloc]initWithFormat:@"价格：￥ %@",[self checkValue:[dict objectForKey:@"specialPrice"]] ];
        lbcolorname.textColor=backColor;
        lbcolorname.font=[UIFont fontWithName:@"Arial" size:11.0];
        [uv addSubview:lbcolorname];
        
    }
    else{
        UILabel *lbcolorname=[[UILabel alloc]initWithFrame:CGRectMake(110, 70, 200, 20)];
        lbcolorname.text=[[NSString alloc]initWithFormat:@"价格：￥ %@",[self checkValue:[dict objectForKey:@"singlePrice"]] ];
        lbcolorname.textColor=backColor;
        lbcolorname.font=[UIFont fontWithName:@"Arial" size:11.0];
        [uv addSubview:lbcolorname];
    }
    
    
    
    
    
    NSMutableArray *dataPicList=[dict objectForKey:@"dataPicList"];
    if([dataPicList count]>0){
        NSDictionary *pic=[dataPicList objectAtIndex:0];
        UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(30,15, 70,70)];
        iv.accessibilityLabel=[pic objectForKey:@"picPath"];
        [iv setClipsToBounds:YES];
        [iv setContentMode:UIViewContentModeScaleAspectFill];
        iv.layer.masksToBounds=YES;
        iv.layer.cornerRadius = 2.0;
        iv.userInteractionEnabled = YES;
        [self downloadImg:iv];
        [uv addSubview:iv];
        
       
        
    }
    else{
        UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(30,15, 70,70)];
        iv.image=[UIImage imageNamed:@"nopic"];
        [iv setClipsToBounds:YES];
        [iv setContentMode:UIViewContentModeScaleAspectFill];
        iv.layer.masksToBounds=YES;
        iv.layer.cornerRadius = 2.0;
        iv.userInteractionEnabled = YES;
        [uv addSubview:iv];
        
    }
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemDetailAction:)];
    singleTap.accessibilityLabel=[dict objectForKey:@"itemid"];
    [cell addGestureRecognizer:singleTap];

    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(20, 99, width, 0.6)];
    line.backgroundColor=[UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1.0];
    [uv addSubview:line];

    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.accessoryView=uv;
    
    
    
    return cell;
}


-(NSString *)checkValue:(id)value{
    if(value==nil||value==[NSNull null]){
        return @"-";
    }
    else{
        return value;
    }
}


//查看商品详情
-(void)itemDetailAction:(UITapGestureRecognizer*)sender{
    NSString *storyboard=@"ItemDetail";
    NSString *Identifier=@"ItemDetail";
    UIStoryboard *valueView = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    UIViewController *uv=[valueView instantiateViewControllerWithIdentifier:Identifier];
    UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:uv];
    [uv setValue:@"1" forKey:@"datatypeid"];//不需要强制转换也可以赋值
    [uv setValue:@"" forKey:@"specialvalue"];//不需要强制转换也可以赋值
    [uv setValue:sender.accessibilityLabel forKey:@"formid"];//不需要强制转换也可以赋值
    [self presentModalViewController:baNavigation animated:YES];
    
    //返回
}

- (IBAction)backAction:(UIButton *)sender {
     [self dismissViewControllerAnimated: YES completion: nil ];
}


- (void)downloadImg:(UIImageView *)theImageView {
    
    NSString *imageName=[[NSString alloc]initWithFormat:@"%@?imageView2/2/w/150",theImageView.accessibilityLabel];
    NSURL *imagePath = [NSURL URLWithString:imageName];
    [self.queue addOperationWithBlock: ^{
        
        //在主线程中更新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
            
            [theImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"nopic"]];
            
            
            
        }];
    }];
}

#pragma 删除

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSDictionary *dictO=[itemArray objectAtIndex:[indexPath row]];
        NSString *id=[dictO objectForKey:@"id"];
        NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
        HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
        NSDictionary *dict = @{@"id":id};
        [htnet hmNetWCFmethodName:@"web/memberItemCollection/deleteDataMemberItemCollection" andDic:dict complete:^(NSString *backstr)
         {
             NSDictionary *formdict=[HTFrameNet hmConvertDictFromJsonStr:backstr];//表单单头
             int status=[[formdict objectForKey:@"status"]intValue];
             if(status==0){
                 [HTFrameAlert hmAlertInfo:@"删除成功"];
                 [self BindList];
                 
             }
             else{
                 
             }
             
         }];
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
    }
}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

@end
